package ktmall.servlet;

import com.alibaba.fastjson.JSON;
import com.ktjiaoyu.ktmall.entity.Category;
import com.ktjiaoyu.ktmall.service.CategoryService;
import com.ktjiaoyu.ktmall.service.impl.CategoryServiceImpl;
import com.ktjiaoyu.ktmall.utils.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "CategoryServlet" ,urlPatterns = "/api/category/*")
public class CategoryServlet extends HttpServlet {

    private final CategoryService categoryService = new CategoryServiceImpl();
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String path = request.getPathInfo();
//        R result;
//        if ("/tree".equals(path)){
//            List<Category> tree = categoryService.findTree();
//            result = R.ok(tree);
//        }else {
//            List<Category> list = categoryService.findAll();
//            result = R.ok(list);
//        }
        List<Category> list = categoryService.findAll();
        PrintWriter out = response.getWriter();
        out.print(JSON.toJSONString(list));
        out.flush();
        out.close();

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
